package net.huashitong.zhApp.controller;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.entity.NodeNewfile;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.Project.service.NodeNewFilterService;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.zhApp.service.AppProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value = "/zhApp/projectManage")
public class AppProjectManageController extends BaseCtrl {

    @Lazy
    @Autowired
    private AppProjectsService appProjectsService;
    @Autowired
    private NodeNewFilterService nodeNewFilterService;
    @Lazy
    @Autowired
    private NewProjectsService newProjectsService;
    @Lazy
    @Autowired
    private SupervisionTaskService taskService;

    @ResponseBody
    @RequestMapping(value = "/lists", method = RequestMethod.POST)
    public Map<String, Object> gradePointList(@RequestBody NewProjectsFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", "202");
            result.put("message", "无用户权限");
            return result;
        }

        List<NewProjects> resList = appProjectsService.queryProjectsPageByUser(user, filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("userAuth", user);
        result.put("data", resList);
        result.put("count", filter.getPaging().getRecords());
        return result;
    }


    /**
     * 文件节点清单
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/go/lists", method = RequestMethod.GET)
    public Map<String, List<NodeNewfile>> getNodes(HttpServletRequest request, Model model) {
        Map<String,List<NodeNewfile>> map = new LinkedHashMap<>();
        List<NodeNewfile> list = nodeNewFilterService.queryNodeName();
        for (NodeNewfile file:list) {
            List<NodeNewfile> fileList = nodeNewFilterService.queryNodeNewfile(file);
            map.put(fileList.get(0).getNodeId()+file.getNodeName(),fileList);
        }
        return map;
    }


    /**
     * 滞后项目（进入督查的项目）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/show/laggingProject", method = RequestMethod.GET)
    public Map<String, Object> showLaggingProject() {
        NewProjectsFilter filter = new NewProjectsFilter();
        filter.setStatus("3");
        List<NewProjects> newProjects = new ArrayList<>();
        List<NewProjects> projects = newProjectsService.getProjectInfo4(filter);

        SupervisionTaskFilter supervisionTaskFilter = new SupervisionTaskFilter();
        supervisionTaskFilter.setTaskResult("0");
        supervisionTaskFilter.setValid("1");
        //有效进行中的督查任务
        List<SupervisionTask> tasks = taskService.getTaskListsNoPage(supervisionTaskFilter);
        List<String> projectIds = new ArrayList<>();
        for (SupervisionTask task : tasks) {
            if(!projectIds.contains(task.getProjectId())){
                projectIds.add(task.getProjectId());
            }
        }
        for (NewProjects project : projects) {
            if(projectIds.contains(project.getSysId())){
                newProjects.add(project);
            }

        }
        return coverMessage("200", "成功获取数据", newProjects);
    }
}
